#include "stdafx.h"
#include "systemtime.h"

namespace Clock
{
	SystemTimer::SystemTimer()
	{
		QueryPerformanceFrequency(&myPerformanceFrequency);
		Init();
	}

	SystemTimer::~SystemTimer()
	{
	}

	void SystemTimer::Init()
	{
		QueryPerformanceCounter(&myClockStarted);
		myClockChecked = myClockStarted;
	}

	void SystemTimer::AttachTimer(Timer *aTimer)
	{
		if(aTimer != 0)
		{
			myAttachedTimers.push_back(aTimer);
		}
	}

	bool SystemTimer::Update()
	{
		LARGE_INTEGER counter;

		if(QueryPerformanceCounter(&counter))
		{
			//myCurrentTime.SetTime(static_cast<double>(counter.QuadPart - myClockStarted.QuadPart) / static_cast<double>(myPerformanceFrequency.QuadPart));
			myElapsedTime.SetTime(static_cast<double>(counter.QuadPart - myClockChecked.QuadPart) / static_cast<double>(myPerformanceFrequency.QuadPart));
			myCurrentTime += myElapsedTime;
			myClockChecked = counter;
			for(unsigned int timerIndex=0; timerIndex<myAttachedTimers.size(); timerIndex++)
			{
				myAttachedTimers[timerIndex]->Update(myElapsedTime);
			}
			return true;
		}
		return false;
	}

	Time SystemTimer::GetTotalTime() const
	{
		return myCurrentTime;
	}

	Time SystemTimer::GetElapsedTime() const
	{
		return myElapsedTime;
	}
};
